﻿#include <QApplication>
#include <QLabel>
#include <QImage>
#include <QDebug>
#include <opencv2/opencv.hpp>

QImage cvMatToQImage(const cv::Mat &mat) {
    if(mat.type() == CV_8UC1) {
        QImage image(mat.data, mat.cols, mat.rows, mat.step, QImage::Format_Grayscale8);
        return image;
    } else if(mat.type() == CV_8UC3) {
        QImage image(mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
        return image.rgbSwapped();
    } else if(mat.type() == CV_8UC4) {
        QImage image(mat.data, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
        return image;
    } else {
        return QImage();
    }
}

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    // Load image using OpenCV
    cv::Mat image = cv::imread("C:/Users/Administrator/OneDrive/YourName/src.jpg");

    if(image.empty()) {
        qDebug() << "Could not open or find the image!";
        return -1;
    }

    // Convert OpenCV Mat to QImage
    QImage qImage = cvMatToQImage(image);

    // Display the image in a QLabel
    QLabel label;
    label.setPixmap(QPixmap::fromImage(qImage));
    label.show();

    return app.exec();
}

